package com.nf.service;

import com.nf.entity.House;
import com.nf.entity.HouseInfo;
import com.nf.entity.Student;
import com.nf.entity.StudentHouseInfo;
import com.nf.mappers.StudentMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

public class StudentServiceImpl implements StudentService {

    private StudentMapper studentMapper = null;


    public Student test() {
        //还没有神，于是写以下几行代码
        SqlSession session = null;
        try {
            //高度的耦合（后面要想办法彻底的解构）
            InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
            SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            session = sessionFactory.openSession();
            //直接创建一个dao层的实现类
            studentMapper = session.getMapper(StudentMapper.class);
            System.out.println("我真的很想知道这个实现类是谁："+studentMapper);

        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }

        List<House> houseList = studentMapper.getAllHouseDetail();
        System.out.println(houseList.size());
        for (House h:houseList) {
            System.out.println(h.getId()+"  "+h.getName()+" 人数:"+h.getStudentList().size());
            System.out.print("   学生明细：");
            for (Student s:h.getStudentList()){
                System.out.print(s.getName());
            }
            System.out.println();
        }


        //Map<String,Object> student = studentMapper.findStudentById(2);
        //System.out.println(  student.get("dob") ) ;

        //Student s = studentMapper.findAllStudents();
        //System.out.println(s.getStudId()+"  "+s.getName());
        /*
        List<Student> studentList = studentMapper.findAllStudents();
        for (Student s:studentList) {
            System.out.println(s.getStudId()+"  "+s.getName());
        }*/
        /*


        Student a = new Student();
        a.setStudId(3);
        a.setName("小玲");
        a.setEmail("sss@qq.com");

        studentMapper.insertStudent(a);


        System.out.println(a.getStudId());

        student.setName("小红new");
        studentMapper.updateStudent(student);
        */
        //session.commit();
        //List<StudentHouseInfo> infos = studentMapper.getAllStudentHouseInfo();

       /*
        List<HouseInfo> infos = studentMapper.getAllHouseInfo();
        for (HouseInfo hi:infos) {
            System.out.print(hi.getHid()+"   "+hi.getName()+" "+hi.getTcount()+" "+hi.getKzl());
            System.out.print("(");

            List<Student> students2 = hi.getStudentList();
            for (Student x : students2) {
                System.out.print(x.getName() + ",");
            }


            if (hi.getHid()==1) {

                List<Student> students = studentMapper.findStudentByHouseId(hi.getHid());
                for (Student x : students) {
                    System.out.print(x.getName() + ",");
                }
            }

            System.out.print(")");
            System.out.println();


        } */
        return null;
    }
}
